
import { _decorator } from 'cc';
const { ccclass, property } = _decorator;
import * as cc from 'cc';
import { defaultData } from '../ConfigData';
import { VMCenter } from '../framework/vm/VMCenter';
import { VMBaseAttr, VMEntity } from '../framework/vm/VMEntity';
import { VMTween } from '../framework/vm/VMTransitionValue';
/**
 * Predefined variables
 * Name = ArrayDemo
 * DateTime = Wed Mar 16 2022 12:12:36 GMT+0800 (中国标准时间)
 * Author = 272493431
 * FileBasename = ArrayDemo.ts
 * FileBasenameNoExtension = ArrayDemo
 * URL = db://assets/scripts/scene/ArrayDemo.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('ArrayDemo')
export class ArrayDemo extends cc.Component implements IMVVM{
    _vmTag: string = 'ArrayDemo';
    _vmEntitys: VMEntity[];
    

    start () {
        // [3]
    }

    onLoad(){

    }

    onEnable(){
        VMCenter.add(defaultData,this);
      
        for (let i = 0; i < defaultData.equips.length; i++) {
            let node = cc.find(`Canvas/Label${i+1}`);

            VMCenter.Label(this,node.getComponent(cc.Label),{watchPath :`*.equips.${i}`,vmTween: VMTween(),formator(opts){
                return opts.newValue;
            }});

            // this.schedule(()=>{
            //     defaultData.equips[i] = Math.random() * 200;
            // },i*0.5 + 1);
        }

        // // 数组减少,表现上不会出现变化
        // this.scheduleOnce(()=>{
        //     defaultData.equips.pop();
        //     console.log(`ArrayDemo-> `,JSON.stringify(defaultData.equips));
            
        // },1);
        
        // // 数组减少,表现上不会出现变化
        // this.scheduleOnce(()=>{
        //     // defaultData.equips.push(9);
        //     defaultData.equips[defaultData.equips.length] = 9;
        //     console.log(`ArrayDemo-> `,JSON.stringify(defaultData.equips));
            
        // },2);
        
        // 进行排序之后，表现会按照重新排序的数据显示
        this.scheduleOnce(()=>{
            defaultData.equips.sort((a,b)=>{
                return a-b;
            });
            console.log(`ArrayDemo-> `,JSON.stringify(defaultData.equips));
            
        },2);

    }
    onDisable(){
        VMCenter.remove(this);
    }
    
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
